package normalServer;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

import user.UserInfo;

/*
*Tcp连接的非多线程服务端类
*/
public class Server {
    public List<UserInfo> users = new ArrayList<>();

    public static void main(String[] args) {
        Server server = new Server();
        try {
            // 创建服务器端Socket，即ServerSocket，指定绑定的端口，并监听此端口
            ServerSocket serverSocket = new ServerSocket(5000);
            Socket socket = null;
            System.out.println("等待客户端连接");

            // 循环监听
            while (true) {
                socket = serverSocket.accept();

                // 3、获取输入流，并读取客户端信息
                InputStream in = socket.getInputStream();
                ObjectInputStream objectInputStream=new ObjectInputStream(in);
                HashSet hashSet=(HashSet)objectInputStream.readObject();
                System.out.println(hashSet.size());

                InputStreamReader inputStreamReader = new InputStreamReader(in);
                BufferedReader br = new BufferedReader(inputStreamReader);
                String info = null;
                StringBuilder stringBuilder = new StringBuilder();
                while ((info = br.readLine()) != null) {
                    System.out.println("receive user info:" + info);
                    stringBuilder.append(info);
                }
                socket.shutdownInput();// 关闭输入流

                // 处理用户信息
                StringBuffer stringBuffer = new StringBuffer(stringBuilder.toString());
                int index = stringBuffer.indexOf(",");
                String userID = stringBuffer.substring(0, index);
                String userPWD = stringBuffer.substring(index + 1, stringBuffer.length());
                System.out.println("id:" + userID);
                System.out.println("pwd:" + userPWD);

                String result = userIdent(new UserInfo(userID, userPWD), server);
                System.out.println("result :" + result);

                // 获取输出流，响应客户端的请求
                OutputStream outputStream = socket.getOutputStream();
                PrintWriter printWriter = new PrintWriter(outputStream);
                printWriter.write(result);
                printWriter.flush();

                // 5、关闭资源
                printWriter.close();
                outputStream.close();
                br.close();
                inputStreamReader.close();
                in.close();
                // socket.close();
                // serverSocket.close();
            }
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }

    }

    public Server() {
        register();
    }

    public void register() {
        users.add(new UserInfo("admin", "admin"));
        users.add(new UserInfo("001", "123456"));
        users.add(new UserInfo("002", "123456"));
    }

    public static String userIdent(UserInfo uInfo, Server server) {
        for (UserInfo userList : server.users) {
            if ((uInfo.getUserId()).equals(userList.getUserId())) {
                if ((uInfo.getUserPwd()).equals(userList.getUserPwd())) {
                    return "验证成功";
                } else {
                    return "密码不正确";
                }
            }
        }
        return "用户不存在";
    }
}
